package com.xiaomei.manager.quartz;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;

import com.xiaomei.manager.dao.BroadcastMapper;
import com.xiaomei.manager.dao.BroadcastPushMapper;
import com.xiaomei.manager.dao.FabulousRecordMapper;
import com.xiaomei.manager.dao.JPushRecordMapper;
import com.xiaomei.manager.dao.UserMapper;
import com.xiaomei.manager.model.Broadcast;
import com.xiaomei.manager.model.BroadcastPush;
import com.xiaomei.manager.model.FabulousRecord;
import com.xiaomei.manager.model.JPushRecord;
import com.xiaomei.manager.model.User;
import com.xiaomei.manager.utils.Constants;
import com.xiaomei.manager.web.jpush.JpushUtil;




public class QuartzFabulous {
	
	@Autowired
	private BroadcastMapper broadcastMapper;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private FabulousRecordMapper fbulousRecordMapper;
	@Autowired
	private BroadcastPushMapper broadcastPushMapper;
	@Autowired
	private JPushRecordMapper jPushRecordMapper;
	protected void execute() {
		//当前时间
		Long time = new Date().getTime();
		//起始时间
		Long startTime = getStartTime().getTime(); 
		 //结束司机
		Long endTime =  getEndTime().getTime();   
		if(time<startTime || time>endTime){
			return ;
		}	
		// TODO Auto-generated method stub
		//随机获取一当天发的广播
		Broadcast broadcastFabulous = broadcastMapper.selectBroadcastFabulous();
		if(broadcastFabulous == null) {
			return ;
		}
		//根据广播获取用户信息
		User selectBrodcast = userMapper.selectBrodcast(broadcastFabulous.getUserCode());
		//如果用户是女，则获取虚拟用户为为男的，点赞
		User user = null;
		if(Constants.USER_SEX_GIRL.equals(selectBrodcast.getSex())){
			//获取一天虚拟假数据为男的
			user = userMapper.selectSexBoy();
		}else {
			//获取一天虚拟假数据为女的
			user = userMapper.selectSexFemale();
		}
		//保存到点赞记录表
		now(broadcastFabulous.getId(), user.getUserCode(),broadcastFabulous.getUserCode());
	}
	
	 private boolean now(Integer id, String userCode ,String AcceptCode ) {
		FabulousRecord record =new FabulousRecord();
		record.setBroadcastId(id);
		record.setUserCode(userCode);
		record.setFabulousTime(new Timestamp(System.currentTimeMillis()));	
		if(fbulousRecordMapper.insertSelective(record) >0) {
			//更新广播表点赞数
			broadcastMapper.updateFabulousCountById(id);
			
			BroadcastPush brp=new BroadcastPush();
			//广播ID
			brp.setBroadcastId(record.getBroadcastId());	
			brp.setCommentTime(record.getFabulousTime());
			brp.setSponsorCode(record.getUserCode());
			brp.setUserCode(AcceptCode);			
			//未读
			brp.setState("0");
			//点赞
			brp.setType("1");
			broadcastPushMapper.insertSelective(brp);
			//j 
			if(!AcceptCode.equals(record.getUserCode())){
				JPushRecord jrp=new JPushRecord();
				jrp.setUserCode(AcceptCode);
				jrp.setContent(JpushUtil.MESSAGE_16);
				jrp.setMessageState(JpushUtil.STATE_SUCCESS);
				jrp.setMessageType(JpushUtil.NEW_FABULOUS_RECORD);
				jrp.setPushTime(new Timestamp(System.currentTimeMillis()));
				jrp.setReadState("0");
				jPushRecordMapper.insert(jrp);
//				jpushRecordService.JpushService(jrp);
			}
		}
		return true;
	 }
	 public static void main(String[] args) throws Exception {  
	        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
	        System.out.println(sdf.format(getStartTime()));  
	        System.out.println(sdf.format(getEndTime()));  
	    }  
	 private static Date getEndTime() {  
	        Calendar todayStart = Calendar.getInstance();  
	        todayStart.set(Calendar.HOUR_OF_DAY, 23);  
	        todayStart.set(Calendar.MINUTE, 59);  
	        todayStart.set(Calendar.SECOND, 59);  
	        todayStart.set(Calendar.MILLISECOND, 999);  
	        return todayStart.getTime();  
	    }  
	  
	    private static Date getStartTime() {  
	        Calendar todayEnd = Calendar.getInstance();  
	        todayEnd.set(Calendar.HOUR_OF_DAY, 9);  
	        todayEnd.set(Calendar.MINUTE, 0);  
	        todayEnd.set(Calendar.SECOND, 0);  
	        todayEnd.set(Calendar.MILLISECOND, 0);  
	        return todayEnd.getTime();  
	    }  
}
